/**
 * 
 */
package dstation.client;

import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.shared.HandlerRegistration;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;

/**
 * @author Evan
 *
 */
public class DialogPrompt extends DialogBox {

	private final Button enterButton = new Button("Submit");
	private final Button closeButton = new Button("Close");
	private final TextBox nameBox = new TextBox();
	private final VerticalPanel dialogVPanel = new VerticalPanel();
	private final FlowPanel dialogFPanel = new FlowPanel();
	private HandlerRegistration enterReg;
	private HandlerRegistration closeReg;


	public DialogPrompt(String title) {
		super(false,true);
		super.setText(title);
		super.setAnimationEnabled(true);
		super.setGlassEnabled(true);

		
		enterButton.getElement().setId("enterButton");
		closeButton.getElement().setId("closeButton");
		
		dialogVPanel.add(nameBox);
		dialogVPanel.add(dialogFPanel);
		dialogFPanel.add(enterButton);
		dialogFPanel.add(closeButton);
		super.setWidget(dialogVPanel);
	}

	public void show(int left, int top){
		nameBox.setText("");
		super.setPopupPosition(left, top);
		super.show();
	}
	
	public void onSubmit(ClickHandler c){
		if(enterReg!=null){
			enterReg.removeHandler();
		}
		enterReg = enterButton.addClickHandler(c);
	}
	public void onCancel(ClickHandler c){
		if(closeReg!=null){
			closeReg.removeHandler();
		}
		closeReg = closeButton.addClickHandler(c);
	}
	
	public String getAnswer(){
		if(nameBox.getText()==""){
			return null;
		}
		else{
			return nameBox.getText();
		}
	}
}
